<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
  <%include("/common/header.html",{title:''}){}%>
  <title>开设企业</title>
  <style>
  </style>
</head>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=IENn7ABARjQIkTeAKyh80VTkg2jYXf3K"></script>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:">首页</a><span lay-separator="">/</span>
        <a><cite>开设企业</cite></a>
      </span>
  <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
     href="javascript:location.replace(location.href);" title="刷新">
    <i class="layui-icon layui-icon-refresh-3"></i>
  </a>
</div>

<div class="x-body" style="margin: 20px auto auto 15px;">
  <div class="layui-row">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">
        <div class="layui-input-inline">
          <input type="text" id="search_term" name="search_name" placeholder="请输企业名称" autocomplete="off"
                 class="layui-input time_sreach">
        </div>
        <div class="layui-input-inline">
          <button class="layui-btn layui-btn-checked" lay-submit="" lay-filter="LAY-user-front-search" id="searchBtn"><i
                  class="layui-icon layui-icon-search"></i>查询
          </button>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
    <script type="text/html" id="toolbarDemo">
      <button class="layui-btn layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加企业
      </button>
    </script>
    <script type="text/html" id="table-useradmin-webuser">
      <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
              class="layui-icon layui-icon-edit"></i>修改</a>
      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      <!--  <a class="layui-btn   layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-cols"></i>数据权限</a>-->
    </script>

  </div>
</div>
<!-- 弹出页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
  <form action class="layui-form" lay-filter="component-form-element" id="myform">
    <!--    <div class="layui-btn-container">
          <button type="button" class="layui-btn layui-btn-sm" lay-demo="getChecked">获取选中节点数据</button>
          <button type="button" class="layui-btn layui-btn-sm" lay-demo="setChecked">勾选指定节点</button>
          <button type="button" class="layui-btn layui-btn-sm" lay-demo="reload">重载实例</button>
        </div>-->
    <input type="hidden" name="id">
    <div class="layui-form-item">
      <label class="layui-form-label">企业名称：</label>
      <div class="layui-input-inline">
        <input type="text" name="departName" required lay-verify="required" placeholder="请输入企业名称" autocomplete="off"
               class="layui-input">
      </div>

      <label class="layui-form-label">联系人名：</label>
      <div class="layui-input-inline">
        <input type="text" name="contacts" required lay-verify="required" placeholder="请输入联系人名" autocomplete="off"
               class="layui-input">
      </div>
      <label class="layui-form-label">联系方式：</label>
      <div class="layui-input-inline">
        <input type="tel" name="mobile" required lay-verify="required" placeholder="请输入联系方式" autocomplete="off"
               class="layui-input">
      </div>
      <label class="layui-form-label">传真邮箱：</label>
      <div class="layui-input-inline">
        <input type="text" name="email" required lay-verify="required" placeholder="请输入传真邮箱" autocomplete="off"
               class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">注册资金：</label>
      <div class="layui-input-inline">
        <input type="text" name="capital" required lay-verify="required" placeholder="请输入注册资金" autocomplete="off"
               class="layui-input">
      </div>

      <label class="layui-form-label">创建时间：</label>
      <div class="layui-input-inline">
        <input type="text" name="CreateDate" id="test1" required lay-verify="required" placeholder="请选择创建时间"
               autocomplete="off"
               class="layui-input">
      </div>
      <label class="layui-form-label">法人姓名：</label>
      <div class="layui-input-inline">
        <input type="tel" name="CreateName" required lay-verify="required" placeholder="请输入法人姓名" autocomplete="off"
               class="layui-input">
      </div>
      <label class="layui-form-label">关联版本：</label>
      <div class="layui-input-inline">
        <select name="systemVersion" id="verison" required lay-verify="required" autocomplete="off"
                class="layui-input">

        </select>
      </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">企业账号：</label>
      <div class="layui-input-inline">
        <input type="text" name="loginaccount" id="loginaccount" required lay-verify="required" placeholder="请输入企业登陆账号" autocomplete="off"
               class="layui-input">
      </div>

      <label class="layui-form-label">企业密码：</label>
      <div class="layui-input-inline">
        <input type="password" name="loginpsd" id="loginpsd"  required lay-verify="required" placeholder="输入企业登录密码"
               autocomplete="off"
               class="layui-input">
      </div>

    </div>

    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">企业描述：</label>
      <div class="layui-input-block">
        <textarea placeholder="请输入内容" name="description" class="layui-textarea"></textarea>
      </div>
    </div>


    <div class="layui-form-item">
      <div class="layui-input-block" style="margin-left: 240px;">
        <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>
      </div>
    </div>
  </form>
</div>


<!-- 弹出查看页面 -->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin1"
     style="padding: 15px;display: none">
  <form action class="layui-form" lay-filter="component-form-element1" id="myform1">

  </form>
</div>
<%include("/common/foot.html"){}%>
<script>

  layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui', 'AliangCommon', 'tree', 'util'], function () {
    var $ = layui.$;
    var table = layui.table;
    var form = layui.form;
    var tree = layui.tree;
    var util = layui.util;
    var laydate = layui.laydate;
    var AjaxCommon = layui.AjaxCommon;
    var AliangLayui = layui.AliangLayui;
    var AliangCommon = layui.AliangCommon;

    var title = "添加企业";
    var findAll_url = 'enterprise/list.json';
    var update_url = 'enterprise/update.json';
    var delete_url = 'enterprise/delete.json';

    var index;//定义建立富文本编辑器全局变量
    var layedit;//定义富文本编辑器layedit全局变量
    var cols = [ //表头
      /*{checkbox: true, fixed: true},
      {
        field: 'createDate',

        templet: "<div>{{layui.util.toDateString(d.createDate, 'yyyy-MM-dd ')}}</div>",
        title: '发布时间',
        fixed: 'left',
      },*/
      {field: 'departName', sort: 'true', title: '企业名称',},
      {field: 'contacts', sort: 'true', title: '企业联系人',},
      {field: 'email', sort: 'true', title: '邮箱&传真',},
      {field: 'mobile', sort: 'true', title: '联系方式',},
      {field: 'description', sort: 'true', title: '描述',},
      {
        field: 'CreateDate',
        sort: 'true',
        templet: "<div>{{layui.util.toDateString(d.addtime, 'yyyy-MM-dd ')}}</div>",
        title: '创建时间',
      },
      {title: '操作', fixed: 'right', width: 150, toolbar: '#table-useradmin-webuser',}

    ];
    AliangLayui.createTable(
      "#LAY-user-manage",
      "listReload",
      "#toolbarDemo",
      title,
      findAll_url,
      true,
      [10, 15, 20, 25],
      ['filter', 'print', 'exports'],
      cols,
      null
    );


    //监听搜索
    form.on('submit(LAY-user-front-search)', function (data) {
      var field = data.field;
      //执行重载
      table.reload('listReload', {
        where: {
          term: field.search_name
        },
        page: {
          curr: 1//重新从第 1 页开始
        }
      });
    });

    //常规用法
    laydate.render({
      elem: '#test1'
    });

    function getCheckedId(jsonObj) {
      var id = "";
      $.each(jsonObj, function (index, item) {
        if (id != "") {
          id = id + "," + item.id;
        } else {
          id = item.id;
        }
        var i = getCheckedId(item.children);
        if (i != "") {
          id = id + "," + i;
        }
      });
      return id;
    }

    //按钮事件
    util.event('lay-demo', {
      getChecked: function (othis) {
        var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
        var ids = getCheckedId(checkedData);

        layer.alert(JSON.stringify(checkedData), {shade: 0});
        console.log(checkedData);
      }
      , setChecked: function () {
        tree.setChecked('demoId1', [1, 2]); //勾选指定节点
      }
      , reload: function () {
        //重载实例
        tree.reload('demoId1', {});

      }
    });


    //初始化修改页面

    function loadData(data) {

      var ret = {
        "id": data.id,
        "departName": data.departName,
        "contacts": data.contacts,
        "email": data.email,
        "mobile": data.mobile,
        "description": data.description,
        "capital": data.capital,
        "CreateDate": data.createDate,
        "systemVersion": data.systemVersion.id,
        "loginaccount": data.loginaccount,
        "loginpsd": data.loginpsd,
        "CreateName": data.createName
      };
      var loginaccount = document.getElementById("loginaccount");
      loginaccount.setAttribute("readOnly",'true');
       var loginpsd = document.getElementById("loginpsd");
      loginpsd.setAttribute("readOnly",'true');

      form.render();
      return ret;
    }

    AliangCommon.select({
      url: "version/json_version_list.json",
      elemIds: 'verison',
      dataValue: 'id',
      dataText: 'versioname',
      defaultValue: "versioname",//默认选项（可以是具体某个option的value或text）
      otherOption: "<option value=''>选择版本</option>",//额外选项
      isLocalRender: true//开启局部渲染
    });

    //////////////////////////////////////以上是修改每个页面修改的地方
    //弹出修改页面调用初始化数据方法
    function initUpdate(data) {
      var a = layer.open({
        type: 1,
        title: title,
        content: $('#layuiadmin-form-useradmin'),
        btn: ['关闭'], //可以无限个按钮
        cancel: function (index, layero) {//取消事件
          location.replace(location.href);　//layer.open关闭刷新
        },
        success: function (layero, index) {
          layer.iframeAuto(index);
        }
      });
      layer.full(a);


      //如果是添加则重置表单，如果是编辑则给表单赋值
      if (data == 0 || data == null) {
        $('#myform')[0].reset();

      } else {

        layui.form.val("component-form-element", loadData(data));
      }


    }


    //弹出查看页面调用初始化数据方法
    function initUpdate1(data) {
      var a = layer.open({
        type: 1,
        title: title,
        content: $('#layuiadmin-form-useradmin1'),
        btn: ['关闭'], //可以无限个按钮
        cancel: function (index, layero) {//取消事件
          //location.replace(location.href);　//layer.open关闭刷新
        },
        success: function (layero, index) {
          layer.iframeAuto(index);
        }
      });
      layer.full(a);

      //layui.form.val("component-form-element1", loadData(data));
      layer.photos({
        photos: '#fengmian'
        , anim: 5
      });
      layer.photos({
        photos: '#xiangxi'
        , anim: 5
      });
    }


    //修改页面的内容提交到后台处理
    form.on('submit(component-form-element)', function (data) {
      postUpdate(data);
      return false;
    });
    //头工具栏事件
    table.on('toolbar(LAY-user-manage)', function (obj) {
      var checkStatus = table.checkStatus(obj.config.id);
      switch (obj.event) {
        case 'add':
          initUpdate(0);
          break;
        case 'delete':
          var data = checkStatus.data;
          if (data.length > 0) {
            //var id = new Array(); //声明数组
            for (var i in data) {
              var a = data[i].id;//得到复选框的值
              postDelete(a);
            }
            /*layer.confirm('删除以后数据将不能恢复，确定删除吗？', function(index) {
                //alert(data.id);
                postDelete(id);
            });*/
            //alert(id)
            //layer.alert(JSON.stringify(data));
            //layer.close(index);
          } else {
            layer.msg("至少选择一个进行删除");
          }
          break;
        case 'export':
          layer.msg("导出");
          break;
      }
    });

    //点击每行右工具栏“操作”里面按钮效果
    table.on('tool(LAY-user-manage)', function (obj) {
      var data = obj.data;

      if (obj.event === 'edit') {
        initUpdate(data);
      } else if (obj.event === 'view') {
        initUpdate1(data);
      } else if (obj.event === 'del') {

        layer.open({
          id: 1,
          type: 1,
          title: '删除企业 敏感操作 请输入二级密码',
          skin: 'layui-layer-rim',
          area: ['450px', 'auto'],

          content: ' <input type="password" name="pwd" required lay-verify="required" placeholder="请输入二级密码" autocomplete="off"\n' +
            '               class="layui-input">',
          btn: ['保存', '取消'],
          btn1: function (index, layero) {
            postDelete(data.id);
            console.log(layero);
            layer.close(index);
          },
          btn2: function (index, layero) {
            layer.close(index);
          }

        });

        /*layer.confirm('删除以后数据将不能恢复，确定删除吗？', function (index) {
          // alert(data.id);
          postDelete(data.id);
        });*/
      }
    });


    //ajax提交删除内容
    window.postDelete = function (id) {
      AjaxCommon.ajax(delete_url, "post", "json", {"id": id}, function (ret) {
        if (ret == null) {
          layer.msg("删除失败", {
            icon: 5
          });
        } else {
          layer.msg("删除成功", {
            icon: 6
          });
          layer.closeAll();
          table.reload('listReload');
        }
        return ret;
      });
    };

    //ajax提交修改内容
    window.postUpdate = function (data) {
      var formData = new FormData();
      formData.append("data", JSON.stringify(data.field));
      // formData.append("file", $("input[name='file']")[0].files[0]);
      $.ajax({
        url: update_url,
        type: "POST",
        //traditional:true,
        data: formData,
        processData: false,
        contentType: false,
        success: function (data) {
          if (data.code == 0) {
            layer.closeAll();
            layer.msg(data.msg, {time: 600, icon: 1}, function () {
              //table.reload('listReload');
              location.replace(location.href);
            });
          } else {
            layer.msg(data.msg, {icon: 5});
          }
        },
        error: function (jqXHR, textStatus, errorThrown) {
          var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
          layer.open({
            title: "错误信息",
            type: 2,
            content: "base/error.jhtml?info=" + estr,
            btn: ['关闭'] //可以无限个按钮
          });
        }
      });
    }
  });

</script>

</body>

</html>
